﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DoAn.DTO;
using DoAn.BUS;
using System.Data;

namespace DoAn
{
    public partial class TaiKhoanCuaToi : System.Web.UI.Page
    {
        TaiKhoanDTO tk = new TaiKhoanDTO();

        #region "Xử Lí chung"
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadTaiKhoan();
            if (!IsPostBack)
            {
                LoadTrangThaiBanDau();
                LoadThongTinTrang();
            }
        }

        public void LoadThongTinTrang()
        {
            labTenTaiKhoan.Text = tk.TenTaiKhoan;
            txtEmail.Text = tk.Email;


            txtHoTen.Text = tk.HoTen;
            txtDiaChi.Text = tk.DiaChi;
            txtCMND.Text = tk.SoCMND;
            dtNgaySinh.Value = (DateTime)tk.NgaySinh;

            txtTenChuThe.Text = tk.TenChuThe;
            dtNgayHH.Value = (DateTime)tk.NgayHetHan;
            if (tk.LoaiTheNganHang == "Master Card")
            {
                cmbLoaiThe.SelectedIndex = 0;
            }
            else
            {
                cmbLoaiThe.SelectedIndex = 1;
            }
        }

        public void LoadTaiKhoan()
        {
            if (Request.QueryString["tentk"] == null)
            {
                Response.Redirect("TrangChu.aspx");
            }
            else
            {
                TaiKhoanDTO taik = new TaiKhoanDTO();
                taik.TenTaiKhoan = Request.QueryString["tentk"].ToString();
                DataTable dt = new DataTable();
                dt = TaiKhoanBUS.loadTaiKhoan(taik);

                tk.TenTaiKhoan = dt.Rows[0]["TenTaiKhoan"].ToString();
                tk.DiaChi = dt.Rows[0]["DiaChi"].ToString();
                tk.Email = dt.Rows[0]["Email"].ToString();
                tk.HoTen = dt.Rows[0]["HoTen"].ToString();
                tk.MatKhau = dt.Rows[0]["MatKhau"].ToString();
                tk.NgaySinh = (DateTime)dt.Rows[0]["NgaySinh"];
                tk.SoCMND = dt.Rows[0]["SoCMND"].ToString();
                tk.TenChuThe = dt.Rows[0]["TenChuThe"].ToString();
                tk.NgayHetHan = (DateTime)dt.Rows[0]["NgayHetHan"];
                tk.MaThe = dt.Rows[0]["MaThe"].ToString();
            }

        }

        public void LoadTrangThaiBanDau()
        {
            txtEmail.Enabled = false;
            bntLuu.Visible = false;


            txtCMND.Enabled = false;
            txtDiaChi.Enabled = false;
            txtHoTen.Enabled = false;
            dtNgaySinh.Enabled = false;
            txtCMND.Border.BorderStyle = BorderStyle.None;
            txtDiaChi.Border.BorderStyle = BorderStyle.None;
            txtHoTen.Border.BorderStyle = BorderStyle.None;
            bntLuDC.Visible = false;
            bntLuuHoTen.Visible = false;
            bntLuuNgS.Visible = false;


            txtTenChuThe.Enabled = false;
            txtTenChuThe.Border.BorderStyle = BorderStyle.None;
            cmbLoaiThe.Enabled = false;
            dtNgayHH.Enabled = false;
            bntLuuChuThe.Visible = false;
            bntLuuNgHH.Visible = false;
            bntLuuLoaiThe.Visible = false;
        }      
        #endregion

        #region "Xử Lí trang Tài Khoản"
        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Response.Redirect("DoiMatKhau.aspx?tentk=" + tk.TenTaiKhoan);
        }

        protected void btnDoiEmail_Click(object sender, EventArgs e)
        {
            txtEmail.Visible = true;
            bntLuu.Visible = true;
            bntDoiEmail.Visible = false;
            txtEmail.Enabled = true;
            txtEmail.Border.BorderStyle = BorderStyle.NotSet;
        }

        protected void bntLuu_Click(object sender, EventArgs e)
        {
            tk.Email = txtEmail.Text;
            //Response.Write("<script>alert('" + txtEmail.Text + "')</script>");
            TaiKhoanBUS.thayDoiEmail(tk);
            txtEmail.Enabled = false;
            txtEmail.Border.BorderStyle = BorderStyle.None;
            bntDoiEmail.Visible = true;
            bntLuu.Visible = false;
        }

        protected void bntSuaHoTen_Click(object sender, EventArgs e)
        {
            txtHoTen.Enabled = true;
            txtHoTen.Border.BorderStyle = BorderStyle.NotSet;
            bntLuuHoTen.Visible = true;
            bntSuaHoTen.Visible = false;
        }

        protected void bntSuaNgS_Click(object sender, EventArgs e)
        {
            dtNgaySinh.Enabled = true;
            bntLuuNgS.Visible = true;
            bntSuaNgS.Visible = false;
        }

        protected void bntSuaDC_Click(object sender, EventArgs e)
        {
            txtDiaChi.Enabled = true;
            txtDiaChi.Border.BorderStyle = BorderStyle.NotSet;
            bntLuDC.Visible = true;
            bntSuaDC.Visible = false;
        }

        protected void bntLuuHoTen_Click(object sender, EventArgs e)
        {
            tk.HoTen = txtHoTen.Text;
            TaiKhoanBUS.suaHoTen(tk);
            txtHoTen.Enabled = false;
            txtHoTen.Border.BorderStyle = BorderStyle.None;
            bntLuuHoTen.Visible = false;
            bntSuaHoTen.Visible = true;
        }

        protected void bntLuuNgS_Click(object sender, EventArgs e)
        {
            tk.NgaySinh = dtNgaySinh.Date;
            TaiKhoanBUS.suaNgaySinh(tk);
            dtNgaySinh.Enabled = false;
            bntLuuNgS.Visible = false;
            bntSuaNgS.Visible = true;
        }

        protected void bntLuDC_Click(object sender, EventArgs e)
        {
            tk.DiaChi = txtDiaChi.Text;
            TaiKhoanBUS.suaDiaChi(tk);
            txtDiaChi.Enabled = false;
            txtDiaChi.Border.BorderStyle = BorderStyle.None;
            bntLuDC.Visible = false;
            bntSuaDC.Visible = true;
        }

        protected void bntSuaChuThe_Click(object sender, EventArgs e)
        {
            txtTenChuThe.Enabled = true;
            txtTenChuThe.Border.BorderStyle = BorderStyle.NotSet;
            bntLuuChuThe.Visible = true;
            bntSuaChuThe.Visible = false;
        }

        protected void bntSuaLoaiThe_Click(object sender, EventArgs e)
        {
            cmbLoaiThe.Enabled = true;
            bntLuuLoaiThe.Visible = true;
            bntSuaLoaiThe.Visible = false;
        }

        protected void bntSuaNgHH_Click(object sender, EventArgs e)
        {
            dtNgayHH.Enabled = true;
            bntLuuNgHH.Visible = true;
            bntSuaNgHH.Visible = false;
        }


        protected void bntLuuChuThe_Click(object sender, EventArgs e)
        {
            tk.TenChuThe = txtTenChuThe.Text;
            TaiKhoanBUS.suaChuThe(tk);
            txtTenChuThe.Enabled = false;
            txtTenChuThe.Border.BorderStyle = BorderStyle.None;
            bntLuuChuThe.Visible = false;
            bntSuaChuThe.Visible = true;
        }

        protected void bntLuuLoaiThe_Click(object sender, EventArgs e)
        {
            tk.LoaiTheNganHang = cmbLoaiThe.SelectedItem.Text;
            TaiKhoanBUS.suaLoaiThe(tk);
            bntLuuLoaiThe.Visible = false;
            cmbLoaiThe.Enabled = false;
            bntSuaLoaiThe.Visible = true;
        }

        protected void bntLuuNgHH_Click(object sender, EventArgs e)
        {
            tk.NgayHetHan = (DateTime)dtNgayHH.Value;
            TaiKhoanBUS.suaNgayHH(tk);
            bntLuuNgHH.Visible = false;
            bntSuaNgHH.Visible = true;
            dtNgayHH.Enabled = false;
        }

        #endregion

        #region "Xử Lí Mua"


        #endregion
    }
}